(function ($) {
    $.fn.upload = function (opts) {
        var defaults = {
            fileType: ['jpg','png','jpeg','gif'],//上传文件类型
            url: '',//上传地址
            size: 2048,//上传大小
            num:2,// 上传数量,
            loading:$('.loading'),
            onUploadBefore:'',//上传前的回调
            onUploadSuccess:'',// 上传成功后的回调
            onUploadError:''//上传失败的回调
        };
        var options = $.extend(defaults,opts);

        this.each(function (index,ele) {
            var obj = {
                _this:$(ele),
                init:function () {
                    this._this.on('change',function (e) {
                        options.loading.fadeIn();
                        var files = e.target.files;
                        obj.type(files)
                    });
                },
                number:function (files,i) {
                    if(files.length > i){
                        layer.msg("图片不能超过"+i+"张",{icon:2})
                    }else{
                        obj.size(files)
                    }
                },
                size: function (files) {
                    var arr = [];
                    $.each(files,function (i,v) {
                        var size = v.size / 1024;
                        if(size <= options.size){
                            arr.push(size)
                        }
                    });
                    arr.length < files.length ? layer.msg('图片大小超出上限',{icon:2}) : obj.upload(files)
                },
                type:function (files) {
                    var arr = [];
                    for(var i=0;i<files.length;i++){
                        var suff = files[i].name.split('.').pop();
                        for(var j=0;j<options.fileType.length;j++){
                            if(suff === options.fileType[j]){
                                arr.push(suff);
                            }
                        }
                    }
                    arr.length < files.length ? layer.msg('文件类型不符合',{icon:2}) : obj.number(files,options.num);
                },
                upload:function (files) {
                    options.onUploadBefore && options.onUploadBefore(files);
                    var formData = new FormData();
                    $.each(files,function (i,v) {
                        formData.append('file[]', v);
                    });
                    $.ajax({
                        url:options.url,
                        data:formData,
                        type:'post',
                        async:true,
                        processData: false,
                        contentType: false,
                        dataType:'json',
                        success:function (res) {
                            obj._this.val('');
                            options.onUploadSuccess && options.onUploadSuccess(res);
                            options.loading.fadeOut();
                        },
                        error:function (res) {
                            options.onUploadError && options.onUploadError(res)
                        }
                    });
                }
            };
            obj.init();
        });
    }
})(jQuery);